import sun.reflect.generics.tree.Tree;

import java.util.*;

/**
 * 给你一个整数 n ，请你生成并返回所有由 n 个节点组成且节点值从 1 到 n 互不相同的不同 二叉搜索树 。
 *
 * 可以按 任意顺序 返回答案。
 */
public class Test95 {

    public static void main(String[] args) {
        List<TreeNode> ret = generateTrees(3);
        System.out.println();
    }

    static List<TreeNode> ret = new LinkedList<>();
    public static List<TreeNode> generateTrees(int n) {
        for (int i = 1; i <= n; i++) {
            createTree(new TreeNode(i),1,n);
        }
        return ret;
    }

    public static TreeNode createTree(TreeNode root,int left,int right){
        if (root.val <= 0) return null;
        if (left >= right){
            return root;
        }
        root.left = createTree(new TreeNode((root.val-1+left)/2),left,root.val-1);
        root.right = createTree(new TreeNode((right+root.val+1)/2),root.val+1,right);
        ret.add(root);

        return root;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}